Function Reference

_AD_CreateOU

Creates a child OU in the specified parent OU.

#Include <AD.au3>
_AD_CreateOU($sParentOU, $sOU)

 

Parameters

$sParentOU Parent OU where the new OU will be created (FQDN)
$sOU OU to create in the the parent OU (Name without leading "OU=")

 

Return Value

Success: 1
Failure: 0, sets @error to:
    1 - $sParentOU does not exist
    2 - $sOU in $sParentOU already exists
    3 - $sOU is missing
    x - Error returned by SetInfo function (Missing permission etc.)

 

Remarks

This does not create any attributes for the OU. Use function _AD_ModifyAttribute.

 

Related

_AD_CreateUser, _AD_CreateGroup, _AD_AddUserToGroup, _AD_RemoveUserFromGroup

 

Example


#AutoIt3Wrapper_AU3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=Y
; *****************************************************************************
; Example 1
; Creates a new OU in the specified parent OU.
; *****************************************************************************
#include <AD.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

; Open Connection to the Active Directory
_AD_Open()
If @error Then Exit MsgBox(16, "Active Directory Example Skript", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)

Global $sFQDN = _AD_SamAccountNameToFQDN()
Global $sParentOU = StringMid($sFQDN, StringInStr($sFQDN, ",OU=") + 1)
Global $iReply = MsgBox(308, "Active Directory Functions - Example 1", "This script creates a new OU (Organizational Unit) in the specified parent OU." & @CRLF & @CRLF & _
        "Are you sure you want to change the Active Directory?")
If $iReply <> 6 Then Exit

; Enter parent OU and OU to create
#region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Active Directory Functions - Example 1", 714, 124)
GUICtrlCreateLabel("Enter the parent OU (FQDN):", 8, 10, 231, 17)
GUICtrlCreateLabel("Enter the OU to create (without leading OU=):", 8, 42, 231, 17)
Global $IParentOU = GUICtrlCreateInput($sParentOU, 241, 8, 459, 21)
Global $IOU = GUICtrlCreateInput("Test-OU", 241, 40, 459, 21)
Global $BOK = GUICtrlCreateButton("Create OU", 8, 72, 121, 33)
Global $BCancel = GUICtrlCreateButton("Cancel", 628, 72, 73, 33, BitOR($GUI_SS_DEFAULT_BUTTON, $BS_DEFPUSHBUTTON))
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
    Global $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $BCancel
            Exit
        Case $BOK
            $sParentOU = GUICtrlRead($IParentOU)
            Global $sOU = GUICtrlRead($IOU)
            ExitLoop
    EndSwitch
WEnd

; Create a new OU
Global $iValue = _AD_CreateOU($sParentOU, $sOU)
If $iValue = 1 Then
    MsgBox(64, "Active Directory Functions - Example 1", "OU '" & $sOU & "' in Parent OU '" & $sParentOU & "' successfully created")
ElseIf @error = 1 Then
    MsgBox(64, "Active Directory Functions - Example 1", "Parent OU '" & $sParentOU & "' does not exist")
ElseIf @error = 2 Then
    MsgBox(64, "Active Directory Functions - Example 1", "OU '" & $sOU & "' in Parent OU '" & $sParentOU & "' already exists")
ElseIf @error = 3 Then
    MsgBox(64, "Active Directory Functions - Example 1", "Value for OU is missing")
Else
    MsgBox(64, "Active Directory Functions - Example 1", "Return code '" & @error & "' from Active Directory")
EndIf

; Close Connection to the Active Directory
_AD_Close()